package com.cskaoyan.market.service.admin;/*
 *文件名: AdminKeyServiceImpl
 *创建者:wxin
 *创建时间:2024/5/6 22:23
 */

import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.db.domain.MarketKeywordExample;
import com.cskaoyan.market.db.mapper.MarketKeywordMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class AdminKeywordServiceImpl implements AdminKeywordService {
    @Autowired
    MarketKeywordMapper marketKeywordMapper;
    @Override
    public List<MarketKeyword> list(Integer page, Integer limit, String keyword, String url, String sort, String order) {
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        marketKeywordExample.setOrderByClause(sort + " " + order);
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (!StringUtils.isEmpty(url)) {
            criteria.andUrlLike("%" + url + "%");
        }
        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);
        return marketKeywords;
    }

    @Override
    public MarketKeyword addKeyword(MarketKeyword marketKeyword) {
        marketKeyword.setIsHot(false);
        marketKeyword.setIsDefault(false);
        marketKeyword.setSortOrder(100);
        marketKeyword.setAddTime(LocalDateTime.now());
        marketKeyword.setUpdateTime(LocalDateTime.now());
        marketKeyword.setDeleted(false);
        marketKeywordMapper.insert(marketKeyword);
        return marketKeyword;
    }

    @Override
    public MarketKeyword updataKeyword(MarketKeyword marketKeyword) {
        marketKeyword.setUpdateTime(LocalDateTime.now());
        marketKeywordMapper.updateByPrimaryKey(marketKeyword);
        return marketKeyword;
    }

    @Override
    public int logicalDelete(Integer id) {
        int deleteByPrimaryKey = marketKeywordMapper.logicalDeleteByPrimaryKey(id);
        return deleteByPrimaryKey;
    }
}
